package com.ruoyi.hr.service.impl.empinfo;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.ProcedureMapper;
import com.ruoyi.hr.mapper.empinfo.OftenQueryMapper;
import com.ruoyi.hr.service.empinfo.OftenQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class OftenQueryServiceImpl extends BaseController implements OftenQueryService {

    @Autowired
    OftenQueryMapper oftenQueryMapper;

    @Autowired
    ProcedureMapper procedureMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> ageStatistics(Map map) {
        //执行查询前更新表数据(eSP_AgeQuery 100)
        procedureMapper.executeProcedure("eSP_AgeQuery 100");
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.ageStatistics(map);
        return ageData;
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> siLingStatistics(Map map) {
        //执行查询前更新表数据(eSP_CyearQuery 100)
        procedureMapper.executeProcedure("eSP_CyearQuery 100");
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.siLingStatistics(map);
        return ageData;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> gongLingStatistics(Map map) {
        //执行查询前更新表数据(eSP_WyearQuery 100)
        procedureMapper.executeProcedure("eSP_WyearQuery 100");
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.gongLingStatistics(map);
        return ageData;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> xueLiStatistics(Map map) {
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.xueLiStatistics(map);
        return ageData;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> ygflStatistics(Map map) {
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.ygflStatistics(map);
        return ageData;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> ygydStatistics(Map map) {
        procedureMapper.executeProcedure("eSP_turnoverQuery '" + map.get("beginDate") + "','" + map.get("endDate") + "',100");
        startPage();
        List<Map<String, Object>> ageData = oftenQueryMapper.ygydStatistics(map);
        return ageData;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> zhtjStatistics(Map map) {
        procedureMapper.executeProcedure("eSP_QueryDual 100," + map.get("Xflag") + "," + map.get("Yflag"));

        //根据x轴找出全部字段
        List<String> AllFieldByXflag = oftenQueryMapper.findAllFieldByXflag(map.get("Xflag").toString());

        //拼sql
        StringBuffer sql = new StringBuffer();
        sql.append("select yzTitle,");
        for (String field : AllFieldByXflag) {
            sql.append("[" + field + "],");
        }
        sql = sql.deleteCharAt(sql.length() - 1);
        sql.append(" from (select (select title from eVW_QueryDualItem yz where yz.id = t.yid and Flag = '" + map.get("Yflag") + "') yzTitle, ");
        sql.append(" (select title from eVW_QueryDualItem xz where xz.id = t.xid and Flag = '" + map.get("Xflag") + "') xzTitle,xValue ");
        sql.append(" from eQuery_dual t) a PIVOT (max(a.xValue) for a.xzTitle in ( ");
        for (String field : AllFieldByXflag) {
            sql.append("[" + field + "],");
        }
        sql = sql.deleteCharAt(sql.length() - 1);
        sql.append("))b");

        List<Map<String, Object>> Data = oftenQueryMapper.zhtjStatistics(sql.toString());
        return Data;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<String> findAllFieldByXflag(String Xflag) {
        return oftenQueryMapper.findAllFieldByXflag(Xflag);
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> hmdStatistics(Map map,Authority authority) {
        List<Map<String, Object>> ageData = oftenQueryMapper.hmdStatistics(map,authority);
        return ageData;
    }
}
